﻿using Microsoft.Owin;
using Owin;
using CanDoo.Autofac.WebApp;
using CanDoo.Web.WebApp.Initialize;
using CanDoo.Core.Caching;
using CanDoo.Core.Dependency;
using CanDoo.Data.Entity;
using System.Web.Http;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataProtection;
using CanDoo.Admin.Core.Identity;
using System.Data.Entity;
using System.Web;
using CanDoo.Admin.Core.Data;
using CanDoo.Core.Initialize;
using CanDoo.Data.Entity.Logging;

using Mehdime.Entity;
using Microsoft.Owin.Security.DataHandler;
using Microsoft.Owin.Security.DataHandler.Serializer;
using Microsoft.Owin.Security.DataHandler.Encoder;

using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.Owin;
using Autofac.Integration.WebApi;
using Autofac.Integration.WebApi.Owin;
using Microsoft.Owin.Security.OAuth;

[assembly: OwinStartupAttribute(typeof(CanDoo.Oil.WebHost.Startup))]
namespace CanDoo.Oil.WebHost
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {

            ICacheProvider provider = new RuntimeMemoryCacheProvider();
            CacheManager.SetProvider(provider, CacheLevel.First);

            //数据库初始化
            IDatabaseInitializer databaseInitializer = new DatabaseInitializer();
            if (!databaseInitializer.IsDatabaseInitialized())
            {
                databaseInitializer.Initialize();
            }

            IServicesBuilder builder = new ServicesBuilder(new ServiceBuildOptions());
            IServiceCollection services = builder.Build();

            //services.AddScoped<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication);
            services.AddScoped(c => app.GetDataProtectionProvider());
            services.AddScoped<UserManager>();
            services.AddScoped<SignInManager>();

            //services.AddScoped(c => DataSerializers.Ticket);
            //services.AddScoped<ITextEncoder, Base64UrlTextEncoder>();
            //services.AddScoped<IDataProtector>(c => new DpapiDataProtectionProvider().Create("ASP.NET Identity"));
            //services.AddScoped<ISecureDataFormat<AuthenticationTicket>, SecureDataFormat<AuthenticationTicket>>();

            services.AddTransient<DefaultDbContext>();
            services.AddTransient<LoggingDbContext>();
            services.AddScoped<IDbContextFactory, CandooDbContextFactory>();

            services.AddDataServices();
            //services.AddDemoServices(app);

            HttpConfiguration webapiConfig = new HttpConfiguration();
            IIocBuilder iocBuilder = new OwinAutofacIocBuilder(services, app, webapiConfig);

            app.UseCanDoo(iocBuilder);


            ConfigureAuth(app, iocBuilder.ServiceProvider);

            //这一行代码必须放在ConfiureOAuth(app)之后 ,操，就这一点，花我两天+一个通宵啊
            //app.UseWebApi(config);
            //app.UseAutofacWebApi(webapiConfig);
            //app.UseWebApi(webapiConfig);
            //app.UseAutofacMvc();

            #region webapi配置

            //webapiConfig.MapHttpAttributeRoutes();
            ////当webapi作为一owin一部分运行时,下面这两句不能有,不然除了webapi,其它程序验证不了
            ////webapiConfig.SuppressDefaultHostAuthentication();
            ////webapiConfig.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            //webapiConfig.Formatters.Clear();
            //webapiConfig.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
            //webapiConfig.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
            //{
            //    NullValueHandling = Newtonsoft.Json.NullValueHandling.Include //设置忽略值为 null 的属性  
            //};

            ////webapiConfig.Filters.Add(new Oil.WebHost.Areas.Api.Filters.ValidationActionFilter());
            ////webapiConfig.Filters.Add(new Oil.WebHost.Areas.Api.Filters.ApiExceptionFilterAttribute());

            //webapiConfig.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);
            //webapiConfig.EnsureInitialized();
            //HelpPageConfig.Register(webapiConfig);
            #endregion




        }
    }
}
